package pack

import (
	jsoniter "github.com/json-iterator/go"
	"mall_frontend_api/biz/model"
	"mall_frontend_api/biz/model/product"
)

func GetProductInfo(info *model.Product, categoryMap map[int64]*model.Category, productItemList []*model.ProductItem, productSpecList []*model.ProductSpec, productSpecValueList []*model.ProductSpecValue) *product.Product {
	data := product.NewProduct()
	if info == nil {
		return data
	}
	data.ID = info.ID
	data.Name = info.Name
	data.Description = info.Description
	data.ShortInfo = info.ShortInfo
	data.UnitName = info.UnitName
	if c, ok := categoryMap[info.CategoryID]; ok {
		data.CategoryName = c.Name
	}
	var sliderImage []string
	if err := jsoniter.UnmarshalFromString(info.SliderImage, &sliderImage); err == nil {
		var sliderImageArray = make([]string, 0)
		for _, s := range sliderImage {
			sliderImageArray = append(sliderImageArray, GetImage(s))
		}
		data.SliderImage = sliderImageArray
	}
	data.Image = GetImage(info.Image)
	data.Price, data.ItemList = buildProductItemList(productItemList)
	data.SpecList = buildProductSpec(productSpecList, productSpecValueList)

	return data
}

func MGetProduct(dataMap map[int64]*model.Product, categoryMap map[int64]*model.Category, productItemList []*model.ProductItem, productSpecList []*model.ProductSpec, productSpecValueList []*model.ProductSpecValue) map[int64]*product.Product {
	var data = make(map[int64]*product.Product)
	var productItemMap = make(map[int64][]*model.ProductItem)
	for _, item := range productItemList {
		productItemMap[item.ProductID] = append(productItemMap[item.ProductID], item)
	}

	var productSpecMap = make(map[int64][]*model.ProductSpec)
	for _, spec := range productSpecList {
		productSpecMap[spec.ProductID] = append(productSpecMap[spec.ProductID], spec)
	}

	var productSpecValueMap = make(map[int64][]*model.ProductSpecValue)
	for _, value := range productSpecValueList {
		productSpecValueMap[value.ProductID] = append(productSpecValueMap[value.ProductID], value)
	}
	for _, info := range dataMap {
		data[info.ID] = GetProductInfo(info, categoryMap, productItemMap[info.ID], productSpecMap[info.ID], productSpecValueMap[info.ID])
	}
	return data
}

func GetProductList(dataMap []*model.Product, categoryMap map[int64]*model.Category, productItemList []*model.ProductItem, productSpecList []*model.ProductSpec, productSpecValueList []*model.ProductSpecValue) []*product.Product {
	var data = make([]*product.Product, 0)
	if len(dataMap) == 0 {
		return data
	}
	var productItemMap = make(map[int64][]*model.ProductItem)
	for _, item := range productItemList {
		productItemMap[item.ProductID] = append(productItemMap[item.ProductID], item)
	}

	var productSpecMap = make(map[int64][]*model.ProductSpec)
	for _, spec := range productSpecList {
		productSpecMap[spec.ProductID] = append(productSpecMap[spec.ProductID], spec)
	}

	var productSpecValueMap = make(map[int64][]*model.ProductSpecValue)
	for _, value := range productSpecValueList {
		productSpecValueMap[value.ProductID] = append(productSpecValueMap[value.ProductID], value)
	}

	for _, info := range dataMap {
		data = append(data, GetProductInfo(info, categoryMap, productItemMap[info.ID], productSpecMap[info.ID], productSpecValueMap[info.ID]))
	}
	return data
}

func buildProductSpec(productSPecList []*model.ProductSpec, productSpecValue []*model.ProductSpecValue) []*product.ProductSpec {
	var list = make([]*product.ProductSpec, 0)
	for _, spec := range productSPecList {
		var data = product.NewProductSpec()
		data.Name = spec.Name
		data.ID = spec.ID
		data.SpecValue = buildProductSpecValue(productSpecValue, spec.ID)
		list = append(list, data)
	}
	return list
}

func buildProductSpecValue(productSpecValueList []*model.ProductSpecValue, productSpecID int64) []*product.ProductSpecValue {
	var list = make([]*product.ProductSpecValue, 0)
	for _, value := range productSpecValueList {
		if value.ProductSpecID == productSpecID {
			var data = product.NewProductSpecValue()
			data.Name = value.Name
			data.ID = value.ID
			list = append(list, data)
		}
	}
	return list
}

func buildProductItemList(productItemList []*model.ProductItem) (float64, []*product.ProductItem) {
	var list = make([]*product.ProductItem, 0)
	var price float64
	for _, item := range productItemList {
		var temp = product.NewProductItem()
		temp.Image = GetImage(item.ThumbImage)
		temp.MarketPrice = item.MarketPrice
		temp.Price = item.SellPrice
		temp.SKUID = item.SkuID
		temp.StoreNumber = item.StoreNums
		list = append(list, temp)
		if price == 0 || price > item.SellPrice {
			price = item.SellPrice
		}
	}
	return price, list
}
